﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace MLY.LTR
{
  public partial class Client : Bll.BasePage
  {
    protected void Page_Load(object sender, EventArgs e)
    {

      lblSuccessDelete.Visible = false;
      lblErrDelete.Visible = false;
    }

    protected void GridView1_Sorting1(object sender, GridViewSortEventArgs e)
    {
      if (ObjectDataSource1.SelectParameters[1].DefaultValue == null ||
      ObjectDataSource1.SelectParameters[2].DefaultValue == null ||
      ObjectDataSource1.SelectParameters[1].DefaultValue !=
      e.SortExpression || ObjectDataSource1.SelectParameters[2].DefaultValue == "Desc")
      { 
        // Sort direction
        ObjectDataSource1.SelectParameters[2].DefaultValue = "Asce";
      }
      else if (ObjectDataSource1.SelectParameters[2].DefaultValue == "Asce")
      {
        ObjectDataSource1.SelectParameters[2].DefaultValue = "Desc";
      }

      // Which column to sort on.
      ObjectDataSource1.SelectParameters[1].DefaultValue = e.SortExpression;

      // We have to do this organizationRelation we will get an exception
      GridView1.DataBind();
      e.Cancel = true;
    }

    protected void btnDelete_OnClick(object sender, EventArgs e)
    {
      int Client = Int32.Parse(((ImageButton)sender).CommandArgument);

      if (Bll.clsOrganizationRelation.DeleteRelation(Client, SessParam.OrganizationId))
      {

        GridView1.DataBind();
        lblSuccessDelete.Visible = true;
        lblErrDelete.Visible = false;
      }
      else
      {
        lblSuccessDelete.Visible = false;
        lblErrDelete.Visible = true;
      }
    }

    protected void GridView1_PreRender(object sender, EventArgs e)
    {
      if ((GridView1.Rows.Count == 0) && (Session["OrganizationId"] != null))
      {
        divEmptyGrid.Visible = true;
      }
      else
      {
        divEmptyGrid.Visible = false;
      }
    }

    protected void imgAddNewOnNoRows_Click(object sender, ImageClickEventArgs e)
    {
      Response.RedirectTo("ClientsCard.aspx?ClientId=-1");

    }


  }
}
